package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.domain.TreeNode;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.P2pYwyService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/p2pyYwy")
public class P2pYwyContoller extends JsonController {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private P2pYwyService p2pYwyService;
	
	/**
	 * 业务员所属业务分组统计
	 * */
	@RequestMapping("/queryYwysstj")
	public ModelAndView queryYwysstj(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String TJ_START = RequestUtils.getStringParameter(request, "TJ_START");
		String TJ_END = RequestUtils.getStringParameter(request, "TJ_END");
		String TJR = RequestUtils.getStringParameter(request, "TJR");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		JSONObject params = new JSONObject();
		params.put("TJ_START", TJ_START);
		params.put("TJ_END", TJ_END);
		params.put("TJR", TJR);
		params.put("start", start);
		params.put("limit", limit);
		
		List<Map<String, Object>> list = this.p2pYwyService.queryYwysstjList(params);
		int total = this.p2pYwyService.getYwysstjCount(params);
		JSONObject result = new JSONObject();
		result.put("data", list);
		result.put("total", total);
		if (null!=page) {//新版本,传参
			int totalPage=total%limit==0?total/limit:total/limit+1;
			result.put("totalPage", totalPage);
			
		}
		serialize(request, response, result);
		return null;
	}


	/**
	 * 导出业务员所属推荐统计excel
	 * @param request
	 * @param response
	 * @return  导出excel表格
	 * @throws Exception
	 */
	@RequestMapping("exportYwysstjExcelMethod")
	public ModelAndView exportYwysstjExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String TJ_START = RequestUtils.getStringParameter(request, "TJ_START");
		String TJ_END = RequestUtils.getStringParameter(request, "TJ_END");
		String TJR = RequestUtils.getStringParameter(request, "TJR");
		
		JSONObject params = new JSONObject();
		params.put("TJ_START", TJ_START);
		params.put("TJ_END", TJ_END);
		params.put("TJR", TJR);
		
		List<Map<String, Object>> list = this.p2pYwyService.queryYwysstjList(params);
		// 查询数据
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getYwysstjExportSearchResultData(list);
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr()+"推荐人数据统计");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "推荐人数据统计.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
}
